package com.wlyuan.open.remote.consumer;

import com.wlyuan.core.domain.event.DomainEventPublisher;
import com.wlyuan.open.domain.PushBusinessType;
import com.wlyuan.open.domain.PushTask;
import com.wlyuan.open.domain.assembler.PushTaskAssembler;
import com.wlyuan.open.domain.repository.PushTaskRepository;
import com.wlyuan.open.domain.repository.PushTenantRepository;
import com.wlyuan.open.infrastructure.event.OrderChanged;
import com.wlyuan.open.remote.converter.OrderMessageConverter;
import com.wlyuan.open.remote.message.OrderMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

/**
 * @author
 * 订单签收监听事件
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class OrderScoredConsumer {

    private final OrderMessageConverter messageConverter = new OrderMessageConverter();
    private final PushTenantRepository pushTenantRepository;
    private final PushTaskRepository pushTaskRepository;

    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue(value = "queue.open.pusher.order.sign.score"),
                    exchange = @Exchange(value = "exchange.topic.order"), key = "OrderScored")
    })
    public void receiveOrderScored(String message) {
        logger.info("收到订单评分信息消息：{}",message);
        OrderMessage orderSignedMessage = messageConverter.convert(message);
        //判断租户id 是否在对接表中，如果存在，则将数据保存
        Long tenantId = orderSignedMessage.getOrder().getTenantId();
        var pushTenant = pushTenantRepository.ofTenantAndType(tenantId, PushBusinessType.ORDER_SCORED);
        if(pushTenant!=null){
            PushTask pushTask = PushTaskAssembler.merge(orderSignedMessage.getOrder(),pushTenant);
            pushTaskRepository.save(pushTask);
        }
    }


}
